using System.Collections.Generic;
using System.Web.Mvc;
using Headspring.Training.Core.Model;

namespace Headspring.Training.UI.Models
{
	public class EmployeeSelectList : SelectList
	{
		private readonly string _selectedEmployeeUsername;

		public EmployeeSelectList(IEnumerable<Employee> employees)
			: base(employees)
		{
		}

		public EmployeeSelectList(IEnumerable<Employee> employees,
		                          Employee selectedEmployee) : base(employees)
		{
			if (selectedEmployee != null)
			{
				_selectedEmployeeUsername = selectedEmployee.UserName;
			}
		}

		public override IList<ListItem> GetListItems()
		{
			var listItems = new List<ListItem>();
			foreach (Employee employee in Items)
			{
				var item = new ListItem();
				item.Value = employee.UserName;
				item.Text = employee.GetFullName();

				if (employee.UserName == _selectedEmployeeUsername)
				{
					item.Selected = true;
				}

				listItems.Add(item);
			}

			return listItems;
		}
	}
}